#include "playlist.h"

#include"songtagger.h"

#include<QDir>
#include<QFileInfo>

Playlist::Playlist(QObject *parent) : QObject(parent)
{
    m_player = new Player(this);
    m_progression = new Progression;
    connect(m_player, SIGNAL(stopped()), this, SLOT(next()));
    connect(m_player, SIGNAL(playing()), this, SLOT(slotPlaying()));
    m_block = false;
    m_blockTimer = new QTimer(this);
    m_blockTimer->setSingleShot(true);
    connect(m_blockTimer, SIGNAL(timeout()), this, SLOT(releaseBlock()));
}

Playlist::~Playlist()
{
    m_player->stop();
    while(!m_songs.isEmpty())
        delete m_songs.takeFirst();
    delete m_progression;
}

void Playlist::add(Song *song, int pos){
    if(pos < 0 || pos >= m_songs.size()){
        m_songs.push_back(song);
        m_progression->add(m_songs.size() - 1);
        emit added(song, m_songs.size() - 1);
    }else{
        m_songs.insert(pos, song);
        m_progression->add(pos);
        emit added(song, pos);
    }
}

void Playlist::add(const QString &location, int pos){
    QUrl url = QUrl::fromLocalFile(location);
    add(url, pos);
}

bool isAudioSuffix(const QString &s){
    //TODO: Of course, more formats should be added...
    if(s == "mp3" || s == "ogg" || s == "wma" || s == "flac")
        return true;
    return false;
}

void Playlist::add(const QUrl &location, int pos){
    //printf("Playlist::add(%s)\n", location.toLocalFile().toUtf8().data());
    //printf("Playlist::add(%s)\n", location.toLocalFile().toLocal8Bit().data());
    QFileInfo fi(location.toLocalFile());
    if(fi.isDir()){
        QDir dir(location.toLocalFile());
        dir.setFilter(QDir::NoDotAndDotDot|QDir::Files|QDir::Dirs);
        dir.setSorting(QDir::DirsLast|QDir::Reversed);
        //printf("\t is dir with %d files\n", dir.count());
        for(int i = dir.count()-1; i >= 0; --i)
            add(QUrl::fromLocalFile(dir.absoluteFilePath(dir[i])), pos);
    }else if(fi.isFile()){
       // printf("\t is file\n");
        if(isAudioSuffix(fi.suffix())){
            Song *song = new Song(location);
            SongTagger::readTag(location, song->data());
            add(song, pos);
        }
    }//else
       // printf("\t is nothing\n");
}

void Playlist::play(){
    if(m_block)
        return;
    m_block = true;
    m_blockTimer->start(100);

    short int i = m_progression->current();
    printf("Playlist::play(%d)\n", i);
    //m_progression->print();
    if(i > 0 && i < m_songs.size())
        m_player->play(m_songs[ i ]->url());
    else if(m_songs.size())//This isn't really the right thing to do, but then again, this shouldn't happen.
        m_player->play(m_songs.first()->url());
}

void Playlist::pause(){
    m_player->pause();
}

void Playlist::next(){
    if(m_block)
        return;
    if(m_progression->next())
        play();
}

void Playlist::prev(){
    if(m_progression->prev())
        play();
}

void Playlist::stop(){
    m_player->stop();
}

void Playlist::slotPlaying(){
    short int i = m_progression->current();
    printf("Playlist::play(%d)\n", i);
    //m_progression->print();
    if(i > 0 && i < m_songs.size()){
        emit playing(i);
        emit playing(m_songs[i]->data(), i);
    }else if(m_songs.size()){//This isn't really the right thing to do, but then again, this shouldn't happen.
        emit playing(0);
        emit playing(m_songs[0]->data(), 0);
    }
}

void Playlist::releaseBlock(){
    m_block = false;
}
